package mobidever.mygodutch.database.sqlitedal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import mobidever.mygodutch.R;
import mobidever.mygodutch.database.base.SQLiteDALBase;
import mobidever.mygodutch.model.ModelCategory;
import mobidever.mygodutch.utility.DateTools;

public class SQLiteDALCategory extends SQLiteDALBase
{

	public SQLiteDALCategory(Context pContext)
	{
		super(pContext);
	}

	public boolean insertCategory(ModelCategory pModelCategory)
	{
		ContentValues _ContentValues = modelCategoryChangeToContentValues(pModelCategory);
		long _ID = getDatabase().insert(getTableNameAndPK()[0], null,
				_ContentValues);
		pModelCategory.setCategoryID(_ID);
		return _ID >= 0;
	}

	public boolean deleteCategory(String pCondition)
	{
		return getDatabase().delete(getTableNameAndPK()[0], pCondition, null) > 0;
	}

	public boolean updateCategory(ModelCategory pModelCategory)
	{
		long _id = pModelCategory.getCategoryID();
		ContentValues _ContentValues = modelCategoryChangeToContentValues(pModelCategory);
		return getDatabase().update(getTableNameAndPK()[0], _ContentValues,
				"CategoryID=" + _id, null) > 0;
	}

	public boolean updateCategory(ModelCategory pModelCategory,
			String pCondition)
	{
		ContentValues _ContentValues = modelCategoryChangeToContentValues(pModelCategory);
		return getDatabase().update(getTableNameAndPK()[0], _ContentValues,
				"and CategoryID=" + pCondition, null) > 0;
	}
	
	public boolean updateCategory(ContentValues pContentValues,String pCondition)
	{
		return getDatabase().update(getTableNameAndPK()[0], pContentValues, pCondition, null) > 0;
	}

	public List<ModelCategory> getCategorys(String pCondtition)
	{
		String _sql = "select * from " + getTableNameAndPK()[0] + " where 1=1 "
				+ pCondtition;
		ArrayList<ModelCategory> _ArrayList = (ArrayList<ModelCategory>) getList(_sql);
		return _ArrayList;
	}

	private ContentValues modelCategoryChangeToContentValues(
			ModelCategory pModelCategory)
	{
		ContentValues _ContentValues = new ContentValues();
		_ContentValues.put("CategoryName", pModelCategory.getCategoryName());
		_ContentValues.put("CreateDate", DateTools.getFormatDateTime(
				pModelCategory.getCreateDate(), "yyyy-mm-dd hh:mm:ss"));
		_ContentValues.put("State", pModelCategory.getState());
		_ContentValues.put("TypeFlag", pModelCategory.getTypeFlag());
		_ContentValues.put("ParentID", pModelCategory.getParentID());
		_ContentValues.put("Path", pModelCategory.getPath());
		return _ContentValues;
	}

	private void createDefaultCategory(SQLiteDatabase pDatabase)
	{
		ModelCategory _ModelCategory = new ModelCategory();
		
		_ModelCategory.setTypeFlag(getContext().getString(R.string.PayoutTyoeFlag));
		_ModelCategory.setParentID(0);
		_ModelCategory.setPath("");
		
		String[] _CategoryName = getContext().getResources().getStringArray(
				R.array.InitDefaultCategoryName);
		for (int i = 0; i < _CategoryName.length; i++)
		{
			_ModelCategory.setCategoryName(_CategoryName[i]);
			ContentValues _ContentValues = modelCategoryChangeToContentValues(_ModelCategory);
			long _ID = pDatabase.insert(getTableNameAndPK()[0], null,
					_ContentValues);
			_ModelCategory.setPath(_ID + ".");
			_ModelCategory.setCategoryID(_ID);
			
			_ContentValues = modelCategoryChangeToContentValues(_ModelCategory);
			
			pDatabase.update(getTableNameAndPK()[0], _ContentValues, "CategoryID = " + _ID, null);
			
		}
	}

	@Override
	protected String[] getTableNameAndPK()
	{
		return new String[]
		{ "Category", "CategoryID" };
	}

	@Override
	protected Object findModel(Cursor pCursor)
	{
		ModelCategory _ModelCategory = new ModelCategory();
		_ModelCategory.setCategoryName(pCursor.getString(pCursor
				.getColumnIndex("CategoryName")));
		_ModelCategory.setCategoryID(pCursor.getInt(pCursor
				.getColumnIndex("CategoryID")));
		Date _Date = DateTools.getDate(
				pCursor.getString(pCursor.getColumnIndex("CreateDate")),
				"yyyy-MM-dd HH:mm:ss");
		_ModelCategory.setCreateDate(_Date);
		_ModelCategory
				.setState(pCursor.getInt(pCursor.getColumnIndex("State")));
		_ModelCategory.setTypeFlag(pCursor.getString(pCursor
				.getColumnIndex("TypeFlag")));
		_ModelCategory.setParentID(pCursor.getLong(pCursor
				.getColumnIndex("ParentID")));
		_ModelCategory
				.setPath(pCursor.getString(pCursor.getColumnIndex("Path")));
		return _ModelCategory;
	}

	public void onCreate(SQLiteDatabase pDatabase)
	{
		StringBuilder sCreateTableScript = new StringBuilder();

		sCreateTableScript.append("		Create  TABLE Category(");
		sCreateTableScript.append("				[CategoryID] integer PRIMARY KEY AUTOINCREMENT NOT NULL");
		sCreateTableScript.append("				,[CategoryName] varchar(20) NOT NULL");
		sCreateTableScript.append("				,[TypeFlag] varchar(20) NOT NULL");
		sCreateTableScript.append("				,[ParentID] integer NOT NULL");
		sCreateTableScript.append("				,[Path] text NOT NULL");
		sCreateTableScript.append("				,[CreateDate] datetime NOT NULL");
		sCreateTableScript.append("				,[State] integer NOT NULL");
		sCreateTableScript.append("				)");

		pDatabase.execSQL(sCreateTableScript.toString());
		createDefaultCategory(pDatabase);
	}

	public void onUpdate(SQLiteDatabase pDatabase)
	{
		// TODO Auto-generated method stub

	}

}
